# coding=utf-8
'''
(C) Copyright 2021 Steven;
@author: Steven kangweibaby@163.com
@date: 2021-05-31

使用 pygame 播放音频

'''

# import pygame

from chess import Chess
from logger import logger
import system
from PySide6.QtCore import QUrl,QFile,QIODevice
from PySide6.QtMultimedia import QSoundEffect# QAudioSink,QAudioFormat,QAudioDevice,QMediaDevices# QMediaPlayer
player=None

dirpath = system.get_dirpath()
AUDIO_MOVE = str(dirpath / 'audios/move.wav')
AUDIO_CAPTURE = str(dirpath / 'audios/capture.wav')
AUDIO_CHECK = str(dirpath / 'audios/check.wav')
AUDIO_INVALID = str(dirpath / 'audios/invalid.wav')
AUDIO_NEW_GAME = str(dirpath / 'audios/newgame.wav')


def init():
    # global player
    # # pygame.mixer.init()
    # # player = QMediaPlayer()
    # format=QAudioFormat()
    # #Set up the format, eg.
    # format.setSampleRate(8000);
    # format.setChannelCount(1);
    # format.setSampleFormat(QAudioFormat.UInt8);
 
    # info=QAudioDevice(QMediaDevices.defaultAudioOutput());
    # if (not info.isFormatSupported(format)):
    #     logger.info("Raw audio format not supported by backend, cannot play audio.")
    #     return;

    # player = QAudioSink(format)
    pass

def play(audio_type):
    # global player
    if audio_type == Chess.CAPTURE:
        audio = AUDIO_CAPTURE
    elif audio_type == Chess.MOVE:
        audio = AUDIO_MOVE
    elif audio_type == Chess.CHECK:
        audio = AUDIO_CHECK
    elif audio_type == Chess.INVALID:
        audio = AUDIO_INVALID
    elif audio_type == Chess.NEWGAME:
        audio = AUDIO_NEW_GAME
    elif audio_type == Chess.CHECKMATE:
        audio = AUDIO_CHECK
    else:
        return
    effect = QSoundEffect()
    logger.info(audio)
    effect.setSource(QUrl.fromLocalFile(audio))
    # effect.setLoopCount(1)#QSoundEffect.Infinite)
    effect.setVolume(1.0)
    effect.play()
    # logger.info("play audio %s", audio)
    # player.setSource(QUrl.fromLocalFile(audio))
    # player.play()
    # pygame.mixer.music.load(audio)
    # pygame.mixer.music.play()
    # sourceFile=QFile()
    # sourceFile.setFileName(audio);
    # sourceFile.open(QIODevice.ReadOnly);

    # format=QAudioFormat()
    # #Set up the format, eg.
    # format.setSampleRate(8000);
    # format.setChannelCount(1);
    # format.setSampleSize(8);
    # format.setCodec("audio/wav");
    # format.setByteOrder(QAudioFormat.LittleEndian);
    # format.setSampleType(QAudioFormat.UnSignedInt);

    # info=QAudioDeviceInfo(QAudioDeviceInfo.defaultOutputDevice())
    # if (!info.isFormatSupported(format)) {
    #     qWarning() << "Raw audio format not supported by backend, cannot play audio.";
    #     return;
    # }

    # audio.stateChanged.connect(self.handleStateChanged)
    # player.start(sourceFile);
if __name__=="__main__":
    init()
    play(Chess.MOVE)